package com.netwit.shiro.jwt.token.service.impl;

import com.netwit.common.service.CommonService;
import com.netwit.shiro.jwt.JwtConfig;
import com.netwit.shiro.jwt.token.service.SecretService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class SecretServiceImpl implements SecretService {
    @Autowired
    private CommonService commonService;

    @Autowired
    private JwtConfig jwtConfig;

    @Override
    public String secret(String userId) {
        String secret;
        if (!StringUtils.isEmpty(userId)){
            HashMap<String, Object> map = new HashMap<>();
            map.put("whr-userCode", userId);
            map.put("alias", "users");
            Map<String, Object> user = commonService.getOne(map);
            secret = (String) user.get("password");
        } else {
            secret = jwtConfig.getSecret();
        }
        return secret;
    }
}
